package com.ardf.service.impl;

import com.ardf.entity.Number;
import com.ardf.entity.*;
import com.ardf.mapper.*;
import com.ardf.service.ICompetitionService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author ShiYunHao
 * @since 2025-01-06
 */
@Service
public class CompetitionServiceImpl extends ServiceImpl<CompetitionMapper, Competition> implements ICompetitionService {
    private final ProjectMapper projectMapper;
    private final GroupMapper groupMapper;
    private final UnitMapper unitMapper;
    private final AthleteMapper athleteMapper;
    private final AthleteTeamMapper athleteTeamMapper;
    private final TeamMapper teamMapper;
    private final AthleteProjectMapper athleteProjectMapper;
    private final CardMapper cardMapper;
    private final NumberMapper numberMapper;
    private final SigninMapper signinMapper;
    private final BatchMapper batchMapper;
    private final RuleMapper ruleMapper;
    private final AlgorithmMapper algorithmMapper;
    private final GradeMapper gradeMapper;
    private final DetailsMapper detailsMapper;
    private final ResultMapper resultMapper;
    private final CompetitionMapper competitionMapper;

    public CompetitionServiceImpl(ProjectMapper projectMapper, GroupMapper groupMapper, UnitMapper unitMapper,
                                  AthleteMapper athleteMapper, AthleteTeamMapper athleteTeamMapper,
                                  TeamMapper teamMapper, AthleteProjectMapper athleteProjectMapper, CardMapper cardMapper,
                                  NumberMapper numberMapper, SigninMapper signinMapper, BatchMapper batchMapper,
                                  RuleMapper ruleMapper, AlgorithmMapper algorithmMapper, GradeMapper gradeMapper,
                                  DetailsMapper detailsMapper, ResultMapper resultMapper, CompetitionMapper competitionMapper) {
        this.projectMapper = projectMapper;
        this.groupMapper = groupMapper;
        this.unitMapper = unitMapper;
        this.athleteMapper = athleteMapper;
        this.athleteTeamMapper = athleteTeamMapper;
        this.teamMapper = teamMapper;
        this.athleteProjectMapper = athleteProjectMapper;
        this.cardMapper = cardMapper;
        this.numberMapper = numberMapper;
        this.signinMapper = signinMapper;
        this.batchMapper = batchMapper;
        this.ruleMapper = ruleMapper;
        this.algorithmMapper = algorithmMapper;
        this.gradeMapper = gradeMapper;
        this.detailsMapper = detailsMapper;
        this.resultMapper = resultMapper;
        this.competitionMapper = competitionMapper;
    }

    @Override
    @Transactional
    public void deleteAll(List<Long> ids) {
        for (Long competitionId : ids) {
            List<Long> projectIds = projectMapper.selectObjs(new LambdaQueryWrapper<Project>().select(Project::getId)
                    .eq(Project::getCompetitionId, competitionId));

            List<Long> groupIds = groupMapper.selectObjs(new LambdaQueryWrapper<Group>().select(Group::getId)
                    .eq(Group::getCompetitionId, competitionId));

            List<Long> unitIds = unitMapper.selectObjs(new LambdaQueryWrapper<Unit>().select(Unit::getId)
                    .eq(Unit::getCompetitionId, competitionId));

            if (!unitIds.isEmpty()) {
                for (Long unitId : unitIds) {
                    List<Long> athleteIds = athleteMapper.selectObjs(new LambdaQueryWrapper<Athlete>().select(Athlete::getId)
                            .eq(Athlete::getUnitId, unitId));
                    if (!athleteIds.isEmpty()) {
                        //7选手接力队伍
                        athleteTeamMapper.delete(new LambdaQueryWrapper<AthleteTeam>()
                                .in(AthleteTeam::getAthleteId, athleteIds));
                        //8选手项目
                        athleteProjectMapper.delete(new LambdaQueryWrapper<AthleteProject>()
                                .in(AthleteProject::getAthleteId, athleteIds));
                        //9选手指卡
                        cardMapper.delete(new LambdaQueryWrapper<Card>()
                                .in(Card::getAthleteId, athleteIds));

                        //选手补卡
                        cardMapper.deleteByAthId(athleteIds);

                        //10选手签到
                        signinMapper.delete(new LambdaQueryWrapper<Signin>()
                                .in(Signin::getAthleteId, athleteIds));
                        //11选手号码
                        numberMapper.delete(new LambdaQueryWrapper<Number>()
                                .in(Number::getAthleteId, athleteIds));
                        //6选手
                        athleteMapper.deleteByIds(athleteIds);
                    }
                }
                //5接力队伍
                teamMapper.delete(new LambdaQueryWrapper<Team>()
                        .in(Team::getUnitId, unitIds));
                //4单位
                unitMapper.deleteByIds(unitIds);
            }


            if (!projectIds.isEmpty()) {
                //12批次
                batchMapper.delete(new LambdaQueryWrapper<Batch>()
                        .in(Batch::getProjectId, projectIds));
            }
            List<Long> ruleIds=!projectIds.isEmpty()?
                    ruleMapper.selectObjs(new LambdaQueryWrapper<Rule>().select(Rule::getId)
                    .in(Rule::getProjectId, projectIds)):List.of();
            List<Long> algorithmIds=List.of();
            if(!ruleIds.isEmpty()){
                algorithmIds=algorithmMapper.selectObjs(new LambdaQueryWrapper<Algorithm>().select(Algorithm::getId)
                        .in(Algorithm::getRuleId, ruleIds));
            }
            if (!algorithmIds.isEmpty()) {
                //17结果表
                resultMapper.delete(new LambdaQueryWrapper<Result>()
                        .in(Result::getAlgorithmId, algorithmIds));
            }
            //14算法
            algorithmMapper.deleteByIds(algorithmIds);
            //13规则
            ruleMapper.deleteByIds(ruleIds);
            //16详情表
            List<Long> gradeIds = !projectIds.isEmpty() ?
                    gradeMapper.selectObjs(new LambdaQueryWrapper<Grade>().select(Grade::getId)
                            .in(Grade::getProjectId, projectIds)) : List.of();
            if (!gradeIds.isEmpty()) {
                detailsMapper.delete(new LambdaQueryWrapper<Details>()
                        .in(Details::getGradeId, gradeIds));
            }
            //15成绩信息
            gradeMapper.deleteByIds(gradeIds);
            //2项目表
            projectMapper.deleteByIds(projectIds);
            //3组别表
            groupMapper.deleteByIds(groupIds);
        }
        //1赛事表
        competitionMapper.deleteByIds(ids);
    }

    @Override
    public Competition selectById(Long competitionId) {
        return competitionMapper.selectOne(new LambdaQueryWrapper<Competition>().eq(Competition::getId,competitionId));
    }
}
